package com.zjsru.plan2024.normal;

/**
 * @Author: cookLee
 * @Date: 2024-01-29
 * 验证回文串
 */
public class IsPalindrome {

    /**
     * 主
     * \
     * 输入: s = "A man, a plan, a canal: Panama"
     * 输出：true
     * 解释："amanaplanacanalpanama" 是回文串。
     * \
     * 输入：s = "race a car"
     * 输出：false
     * 解释："raceacar" 不是回文串。
     * \
     * @param args args
     */
    public static void main(String[] args) {
        IsPalindrome isPalindrome = new IsPalindrome();
        String s = "A man, a plan, a canal: Panama";
        System.out.println(isPalindrome.isPalindrome(s));
    }

    /**
     * 模拟
     *
     * @param s s
     * @return boolean
     */
    public boolean isPalindrome(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                sb.append(Character.toLowerCase(c));
            }
        }
        String sbString = sb.toString();
        String copySb = sb.reverse().toString();
        return sbString.equals(copySb);
    }

}
